import java.util.Scanner;

public class Calculation {
    public static void main(String[] args) {
        int n;
        int count = 0;
        int sum = 0;
        int sum1 = 0;
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        int [] target = new int[n+1];
        int [] arr = new int[n+1];
        int[] last = new int[n];
        int[][] end = new int[n][n];
        for(int i = 0;i < arr.length-1;i++){
            arr[i] = scanner.nextInt();
        }
        for (int j = 0; j < arr.length-1; j++) {
            if (arr[j] > 0 && arr[j] + arr[j + 1] > 0) {
  
                int t = 0;
                int k = j;
                int c;
                for (; k < arr.length - 1; k++) {
                    if(sum1 < 0) {
                        sum1 = 0;
                        break;
                    }
                    if (arr[k] >= 0) {
                        target[t] = arr[k];
                        sum += arr[k];
                        t++;
                    } else {
                        for (int m = k; m < arr.length - 1; m++) {
                            sum1 += arr[m];
                            if (sum1 >= 0) {
                                sum += sum1;
                                for (c = k; c <= m; c++) {
                                    target[t] = arr[c];
                                    t++;
                                }
                                k = c - 1;
                                break;
                            }
                        }
                    }
                }
                last[j] = sum;
                sum = 0;
                for (int i = 0; i < arr.length - 1; i++) {
                    end[count][i] = target[i];
                    target[i] = 0;
                }
                count++;

            }
        }

        int max = last[0];
        for (int i = 0; i < arr.length - 1; i++) {
            if(last[i] > max) {
                max = last[i];
            }
        }


        int sum_two = 0;
        int low = 0;
        int[] arr1 = new int[n];
        for (int i = 0; i < target.length - 1; i++) {
            for (int j = 0; j < target.length - 1; j++) {
              sum_two += end[i][j];
            }
            arr1[i] = sum_two;
            sum_two = 0;
        }
        int max_two = arr1[0];
        for (int i = 0; i < arr1.length; i++) {
            if(max_two < arr1[i])
                low = i;
        }

        for (int i = 0; i < arr.length-1; i++) {
            target[i] = end[low][i];
        }

        for (int i = 0; i < arr.length-1; i++) {
            if(max < arr[i]) {
                max = 0;
                for (int j = 0; j < arr.length-1; j++) {
                    target[j] = 0;
                }
            }
        }
            if(max==0){
                int max1 = arr[0];
                for (int i = 0; i < arr.length-1; i++) {
                    if(arr[i] >= max1){
                        max1 = arr[i];
                    }
                }
                max = max1;
                target[0] = max;
            }
        System.out.println();
        for (int i = 0; i < target.length-1; i++) {
            System.out.print(target[i]+ " ");
        }

        System.out.println();
        System.out.println(max);
    }

}
